﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace MTQ.Model
{
    public class MTQFrameworkEntities : DbContext
    {
        public MTQFrameworkEntities()
             : base("MTQFrameworkEntities")
        {
            this.Configuration.ProxyCreationEnabled = false;
            Database.SetInitializer<MTQFrameworkEntities>(null);
        }


        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            //modelBuilder.RegisterEntityType(typeof(Base_UserInfo));
            //modelBuilder.Configurations.Add(new Base_UserInfoMap());

            string fname = Assembly.GetExecutingAssembly().CodeBase.Replace("file:///", "");
            Type[] types = Assembly.LoadFrom(fname).GetTypes();

            //string assembleFileName = Assembly.GetExecutingAssembly().CodeBase.Replace("Excalibur.DataBase.SqlServer.DLL", "Excalibur.Application.Mapping.DLL").Replace("file:///", "");

            var typesToRegister = types.Where(type => !String.IsNullOrEmpty(type.Namespace))
            .Where(type => type.BaseType != null && type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));
            foreach (var type in typesToRegister)
            {
                dynamic configurationInstance = Activator.CreateInstance(type);
                modelBuilder.Configurations.Add(configurationInstance);
            }

            base.OnModelCreating(modelBuilder);
        }

    }
}
